package cn.minhx.infrastructure.dcc;

import cn.minhx.types.annotations.DCCValue;
import cn.minhx.types.common.Constants;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 动态配置服务
 *
 * @author min
 */
@Service
public class DCCService {

	/** 降级开关 0关闭、1开启 */
	@DCCValue("downgradeSwitch:0")
	private String downgradeSwitch;

	@DCCValue("cutRange:100")
	private String cutRange;

	@DCCValue("scBlacklist:s02c02")
	private String scBlacklist;

	public boolean isDowngradeSwitch() {
		return "1".equals(downgradeSwitch);
	}

	public boolean isCutRange(String userId) {
		// 计算哈希码的绝对值
		int hashCode = Math.abs(userId.hashCode());
		// 获取最后两位
		int digits = hashCode % 100;
		// 判断是否在切量范围内
		return digits < Integer.parseInt(cutRange);
	}

	/**
	 * 判断黑名单拦截渠道，true 拦截、false 放行
	 */
	public boolean isSCBlackIntercept(String source, String channel) {
		List<String> list = Arrays.asList(scBlacklist.split(Constants.COMMA));
		return list.contains(source + channel);
	}
}
